package cn.huapu.power.energy.controller;

import cn.huapu.power.energy.bean.PeaHistoryex20191M15;
import cn.huapu.power.energy.bean.PeaTransformerscore;
import cn.huapu.power.energy.bean.Transformerbaseinfot;
import cn.huapu.power.energy.bean.Transformerworkdatat;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.bean.vo.*;
import cn.huapu.power.energy.ex.ErrorCodesException;
import cn.huapu.power.energy.service.*;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.vo.Ret;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author USER
 * @title: TransformerController
 * @projectName energy
 * @description: //变压器整体情况页面相关接口
 * @date 2019-5-2815:41
 */
@RestController
@RequestMapping("/tra")
public class TransformerController {
    private Log log = LogFactory.getLog(TransformerController.class);
    @Autowired
    private TransformerworkdatatService tService;
    @Autowired
    private TransenrgefficiencytService transenrService;
    @Autowired
    private PeaTransformerscoreService pService;
    @Autowired
    private PeaHistoryex20191M15Service p15Service;
    @Autowired
    private TransformerbaseinfotService traService;
    @Autowired
    private PeaPeakvalleyunblnccfcntService fcntService;  
    /**
    * @Author jzx
    * @Description //峰谷差
    * @Date 17:16 2019-5-29
    * @Param [da, session]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/traWholeLoad",method = RequestMethod.POST)
    public Ret traWholeLoad(@RequestBody DateParameterBo da){
        List<TransformerworkdatatVo> transforList = tService.traWholeLoad(da);
        Ret r = new Ret();
        r.OK(transforList);
        return r;
    }

    /**
    * @Author jzx
    * @Description //容载比与最大负载率
    * @Date 17:45 2019-6-14
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/traFzAndRz",method = RequestMethod.POST)
    public Ret traFzAndRz(@RequestBody DateParameterBo da){
        String month = da.getMonth();
        String day = da.getDay();
        da.setMonth(null);
        da.setDay(null);
        List<TransformerworkdatatVo> transforList1 = tService.getFzAndRz(da);
        da.setMonth(month);
        List<TransformerworkdatatVo> transforList2 = tService.getFzAndRz(da);
        da.setDay(day);
        List<TransformerworkdatatVo> transforList3 = tService.getFzAndRz(da);
        Map<String,Object> map = new LinkedHashMap<>(3);
        map.put("year",transforList1);
        map.put("month",transforList2);
        map.put("day",transforList3);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
    * @Author jzx
    * @Description //变压器经济得分
    * @Date 10:55 2019-6-13
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/traEconomicsScore",method = RequestMethod.POST)
    public Ret traEconomicsScore(@RequestBody DateParameterBo da){
    	 Integer year = pService.getMaxYear(da.getId());
    	 Ret r = new Ret();
    	 if(year != null) {
    		 Integer month = pService.getMaxMonth(year, da.getId());
	        //根据当前时间获取上月时间
	    	 LocalDate lastMonth = LocalDate.of(year, month, 1);
	         LocalDate lastTwoMonth = lastMonth.minusMonths(1);
	        //获取当月的得分合计与当月目标分
	        List<PeaTransformerscore> list1 = pService.getEconomicsScoreDy(String.valueOf(year),String.valueOf(month),da.getId());
	        //获取上月的得分合计
	        List<PeaTransformerscore> list2 = pService.getEconomicsScoreSy(String.valueOf(lastTwoMonth.getYear()),String.valueOf(lastTwoMonth.getMonthValue()),da.getId());
	        Map<String,Object> map = new LinkedHashMap<>(2);
	        map.put("dy",list1);
	        map.put("sy",list2);
	        r.OK(map);
    	 }
       
        
        return r;
    }

    private Map<String,String> getSyDate(String time) {
        String[] split = time.split("-");
        String year = split[0];
        String month="";
        if(split.length>=2){
            month = split[1].replace("0","");
        }
        String year1;
        String month1;
        if(Integer.parseInt(month)-1<=0){
            year1 = Integer.parseInt(year)-1+"";
            month1 = Integer.parseInt(month)+12+"";
        }else{
            year1=year;
            month1=Integer.parseInt(month)-1+"";
        }
        Map<String,String> map = new HashMap<>(4);
        map.put("year1",year1);
        map.put("month1",month1);
        map.put("year",year);
        map.put("month",month);
        return map;
    }

    /**
    * @Author jzx
    * @Description //变压器经济得分排名
    * @Date 14:31 2019-7-3
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/traByqPm",method = RequestMethod.POST)
    public Ret traByqPm(){
        DateParameterBo da = new DateParameterBo();
        LocalDate localDate = LocalDate.now().minusMonths(1);
        da.setYear(String.valueOf(localDate.getYear()));
        da.setMonth(String.valueOf(localDate.getMonthValue()));
        List<PeaTransformerscore> list = pService.getTraByqPm(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
    * @Author jzx
    * @Description //配电房与变压器下拉名称
    * @Date 15:46 2019-5-30
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/traPdfAndByq",method = RequestMethod.POST)
    public Ret traPdfAndByq(@RequestBody DateParameterBo da){
        List<PdfAndByq> pdfAndByq = tService.getPdfAndByq(da);
        Ret r = new Ret();
        r.OK(pdfAndByq);
        return r;
    }


    /**
    * @Author jzx
    * @Description //负载率（cos实际VScos=0.95）（包括变压器负荷曲线，综合功率损耗）
    * @Date 9:17 2019-5-31
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realTimePower95",method = RequestMethod.POST)
    public Ret realTimePower95(@RequestBody DateParameterBo da){
        //求出类型为S总的数据
        HashMap<String,Double> peaS = p15Service.getSzDatas(da);
        if(ObjectHelper.isEmpty(peaS)){
            throw new ErrorCodesException("查询数据为空",ErrorCodesException.DATA_ERR);
        }
        //求出类型为P总的数据
        HashMap<String,Double> peaP = p15Service.getPzDatas(da);
        if(ObjectHelper.isEmpty(peaP)){
            throw new ErrorCodesException("查询数据为空",ErrorCodesException.DATA_ERR);
        }

        List<Double> list1 = new LinkedList<>();
        List<Double> list2 = new LinkedList<>();
        List<Double> list3 = new LinkedList<>();
        List<Double> list4 = new LinkedList<>();
        List<Double> list5 = new LinkedList<>();
        //求出变压器基础信息中的变压器容量值
        Transformerbaseinfot tr =  traService.getByqrl(da);
        if(ObjectHelper.isEmpty(tr)){
            throw new ErrorCodesException("查询数据为空",ErrorCodesException.DATA_ERR);
        }
        //求出额定负载率损耗与负载波动系数
        int ra = tr.getRatedcapacity();

        Transformerworkdatat trdatat = tService.getFz(da);
        double lwlcoeff = 0;
        if(ObjectHelper.isNotEmpty(trdatat)){
            lwlcoeff  = trdatat.getLwlcoeff();
        }
        for (int i = 0; i < 96; i++) {
            double v = 0;
            double v1 = 0;
            if(ObjectHelper.isNotEmpty(peaS)&&ObjectHelper.isNotEmpty(peaP)){
                //负载率柱状图1
                v = peaS.get("value"+i) / tr.getRatedcapacity();
                //负载率柱状图2
                v1 = peaP.get("value"+i) / tr.getRatedcapacity()/0.95;
            }
            //综合功率损耗率折线图
            double v2 = (Math.pow(v1, 2) - Math.pow(v, 2)) * ra * lwlcoeff;
            //综合功率损耗率柱状图
            double v3 = v2*15/60;

            list1.add(v);
            list2.add(v1);
            list3.add(v2);
            list4.add(v3);
            list5.add(peaP.get("value"+i));
        }
        //经济运行上限（下限）
        List<Transformerworkdatat1Vo> tra = tService.getLoadRate(da);
        Map<String,Object> map = new LinkedHashMap<>(6);
        map.put("fhqx",list5);
        map.put("peaS",list1);
        map.put("peaP",list2);
        map.put("peaGvzx",list3);
        map.put("peaPGvzz",list4);
        map.put("tra",tra);
        Ret r = new Ret();
         r.OK(map);
        return r;
    }

    /**
    * @Author jzx
    * @Description //电费节省,当前日之前所有天的数据
    * @Date 11:00 2019-6-14
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realDfjs",method = RequestMethod.POST)
    public Ret realDfjs(@RequestBody DateParameterBo da){
       List<PeaHistoryex20191M15Vo> list =  p15Service.getFeesSave(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
    * @Author jzx
    * @Description //累计损耗电量及电费
    * @Date 11:26 2019-6-14
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realShFeesAndelec",method = RequestMethod.POST)
    public Ret realShFeesAndelec(@RequestBody DateParameterBo da){
        PeaPeakvalleyunblnccfcntVo fcnt = fcntService.getShFeesAndelec(da);

        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("fcnt",fcnt);
//        map.put("tr",tr);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
    * @Author jzx
    * @Description //综合功率损耗变化
    * @Date 9:20 2019-6-24
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realZhgv",method = RequestMethod.POST)
    public Ret realZhgv(@RequestBody DateParameterBo da){
        List<PeaPeakvalleyunblnccfcntVo> tr = tService.getZhGv(da);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("tr",tr);
        Ret r = new Ret();
        r.OK(map);
        return r;

    }

    /**
    * @Author jzx
    * @Description //综合损耗功率及电量变化
    * @Date 17:47 2019-7-8
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realZhgvPower",method = RequestMethod.POST)
    public Ret realZhgvPower(@RequestBody DateParameterBo da){
        List<PeaHistoryex20191M15> list = p15Service.realZhgvPower(da);
        Ret r = new Ret();
        r.OK(list);
        return r;
    }

    /**
    * @Author jzx
    * @Description //电费变化
    * @Date 8:47 2019-7-11
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realLossFee",method = RequestMethod.POST)
    public Ret realLossFee(@RequestBody DateParameterBo da){
       List<PeaHistoryex20191M15Vo> list = p15Service.realLossFee(da);
       Ret r = new Ret();
       r.OK(list);
        return r;
    }


    /**
    * @Author jzx
    * @Description //实际负载损耗与实际空载损耗
    * @Date 13:34 2019-5-31
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/realKzValue",method = RequestMethod.POST)
    public Ret realKzValue(@RequestBody DateParameterBo da){
        List<TransenrgefficiencytVo> transforList = transenrService.getKzValue(da);
        Ret r = new Ret();
        r.OK(transforList);
        return r;
    }

}
